package com.mo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mo.entity.User;
import com.mo.entity.Video;
import com.mo.feignservice.UserFeignService;
import com.mo.service.IVideoService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 视频 前端控制器
 * </p>
 *
 * @author mo
 * @since 2022-08-28
 */
@RestController
@RequestMapping("/video")
public class VideoController {
    @Autowired
    private UserFeignService userFeignService;
    @Autowired
    private IVideoService videoService;
    @GetMapping("/videos")
    public Map<String,Object> selectVideoPage(
            @RequestParam("page") Integer curPage,
            @RequestParam("per_page") Integer pageSize,
            Integer id,
            String name,
            @RequestParam(value = "category_id",required = false) Integer cid,
            @RequestParam(value = "uploader_name",required = false) String username
    ){
        //远程调用user模块，把该up主的id值查询出来。[1,2,3]
        List<User> users = null;
        if(StringUtils.isNotEmpty(username)){
            users = userFeignService.selectByName(username);
        }
        //多条件查询，分页查询视频信息
        Page<Video> page=new Page<>(curPage,pageSize);
        QueryWrapper<Video> wrapper = new QueryWrapper<>();
        if(ObjectUtils.isNotEmpty(id)){
            wrapper.eq("id",id);
        }
        if(ObjectUtils.isNotEmpty(name)){
            wrapper.like("title",name);
        }
        if(ObjectUtils.isNotEmpty(cid)){
            wrapper.eq("category_id",cid);
        }
        if(ObjectUtils.isNotEmpty(users)){
            //stream流操作list集合
            List<Integer> uids = users.stream().map((u) -> {
                return u.getId();
            }).collect(Collectors.toList());
            wrapper.in("uid",uids);
        }
        IPage<Video> videoPage = videoService.pageVideoCateUser(page,wrapper);
        videoPage.getRecords().forEach(System.out::println);
        //返回结果
        Map<String,Object> map=new HashMap<>();
        map.put("total_count",videoPage.getTotal());
        map.put("items",videoPage.getRecords());
        return map;
    }
}

